var o;
avalon.ready(function(){
	var m = "";
	o = avalon.define({
		$id:"root",
		register:false,
        codenumber:false,
//      notice:false,
		wangji:false,
		shouquan:false,
		codeMsg:false,
        setpw:false,
        login:false,
        isaccede:false,
        useerror:false,
        kind:0,  //0为注册，默认为注册
		/*立即注册弹框*/
        OpenRegister:function(){
        	o.register = true; 
        },
        /*打开验证码弹框*/
        OpenCode:function(){
        	
        	$("#span").html("");
        	// 校验手机号
			// 注册点击下一步
			var mobile = $("#mobile").val();
			var msg = "请输入正确手机号";
			var mobileReg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
			// /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/
			if (mobile == null || mobile == ""
					|| !mobileReg.test(mobile.trim())) {
				$("#span").html("<font color='red'>" + msg + "</font>");
				return;
			}
			$.ajax({
				type : "POST",
				dataType : "json",
				url : "/checkMobile",
				data : {
					"mobile" : mobile
				},
				error : function(request) {
					alert("Your request is wrong!!!");
				},
				success : function(data) {
					if (200 == data.code) {
						m = mobile;
			        	o.register = false;
			        	o.codenumber = true;
			        	o.kind=0;
					} else {
						$("#span").html("<font color='red'>" + data.msg + "</font>");
					}
				}
			});
        	
        },
        Getcode:function(obj){
        	/*settime(obj);*/
			// 发送验证码
			$.ajax({
				type : "POST",
				dataType : "json",
				url : "/sendMobileMsg",
				data : {
					"mobile" : m
				},
				error : function(request) {
					alert("Your request is wrong!!!");
				},
				success : function(data) {
					if (200 == data.code) {

					}
				}
			});
			o.codenumber = true;
        },
        /*登录弹框*/
        OpenLogin:function(){
        	o.login = true;
        	o.kind=1;   //数字为1代表是登录点过去的
        },
        /*忘记密码弹框*/
        forgetpass:function(){
        	o.login = false;
        	o.wangji = true;
        	o.codenumber = true;
        },
        /*验证码弹框过渡到设置密码弹框*/
        OpenNotice:function(){
        	if(o.kind==0&&o.isaccede){
        		// 校验验证码
    			var msgCode = $("#msgcode").val();
    			if (isEmpty(msgCode) ) {
    				return;
    			}
    			$.ajax({
    				url : "/checkMobileMsg",
    				type : "post",
    				dataType : "json",
    				data : {
    					"msgCode" : msgCode,
    					"mobile" : m
    				},
    				success : function(data) {
    					if (data.code == 200) {
    						o.codenumber = false;
    		        		o.setpw = true;
    					} else {
    						o.codeMsg=true;
    						return false;
    					}

    				}
    			})
        	}else if(o.kind==0&&!o.isaccede){
        		//未勾选授权
        		o.shouquan = true
        		return false;
        	}else if(o.kind==1&&o.wangji){
        		o.codenumber = false;
				o.setpw = true;
			}else if(o.kind==1&&!o.wangji){
				//登录
				// 校验手机号密码是不是空
				var mobile = $("#loginMobile").val();
				var password = $("#loginPassword").val();
				if (isEmpty(mobile)) {
					return;
				}
				if (isEmpty(password)) {
					return;
				}
				$.ajax({
					url:"login",
					data:{
						"mobile":mobile,
						"password":password,
						"change":2
					},
					type:"post",
					dataType:"json",
					success:function(data){
						if(data.code==200){
							 window.location.href = "/trade/order_list";
						}else if(data.code==4002){
							window.location.href = "/user/index";
						}else{
							o.useerror = true;
							return false;
						}
					}
					
				});
				/*o.codenumber = false;
				location.href="../products/order_list.html";  //跳转至指定的页面
*/			}
        },
        /*是否勾选注册协议*/
        ChooseAccede:function(){
        	o.isaccede = !o.isaccede;
        },
        
        toChicang:function(){
        	//立即注册
        	// 密码立即注册
			var psword = $("#psword").val();
            $("#span").html("");
			$.ajax({
				url : "/register",
				type : "post",
				data : {
					"mobile" : m,
					"password" : psword
				},
				dataType : "json",
				success : function(data) {
					if (data.code == 200) {
						 window.location.href = "/trade/index";
					}else{
                        $("#span").html("<font color='red'>" + data.msg + "</font>");
					}
				}
			})
        	/*o.setpw = false;
        	location.href="../products/order_list.html";  //跳转至指定的页面
*/        },
//      OpenSetpw:function(){
//      	o.notice = false;
//      	o.setpw = true;
//      },
        /*关闭弹框*/
        closepop:function(){
        	o.register = false;
        	o.codenumber = false;
//      	o.notice = false;
        	o.setpw = false;
        	o.login = false;
        }
	});
	avalon.scan(document.body);
})

function limitInput(o) {
	var value = o.value;
	var msg = "密码是6-12位数字、字母组合";
	var reg = /^[0-9a-zA-Z]+$/;
	if (!reg.test(value)) {
		$("#myspan2").html("<font color='red'>" + msg + "</font>");
		return;
	}
	var min = 6;
	var max = 12;
	if (value.length < min || value.length > max) {
		$("#myspan2").html("<font color='red'>" + msg + "</font>");
		return;
	}
}
function isEmpty(obj) {
	if (typeof obj == "undefined" || obj == null || obj.trim() == "") {
		return true;
	} else {
		return false;
	}
}